import {
    fetchLiveDetailReq,
    fetchLiveListReq,
    fetchStarRankListReq,
    fetchStarRankListNoTokenReq,
} from "../apis/goApis"
import {handleResp} from "../utils/commonfunc"

const uid = localStorage.getItem('userid')

export const live = {

    state: {
        // 正在直播列表
        liveList: [],
        // 主播排行榜
        starRankList: [],
        // 当前用户页面直播间详细
        liveDetail: {
            Id: '',
            Cid: '',
            CreateAt: '',
            Uid: '',
            Httpflvaddr: '',
            Rmtpaddr: '',
            Name: '',
            Chattoken: '',
            Uname: '',
            Status: '',
        },
        // 当前推流地址
        pushUrl: '',
        // SDK cid
        cid: '',
    },

    reducers: {
        setPushUrl(state, payload) {
            return {
                ...state,
                pushUrl: payload,
            }
        },

        setSDKCid(state, payload) {
            return {
                ...state,
                cid: payload,
            }
        },

        setLiveDetail(state, payload) {
            return {
                ...state,
                liveDetail: payload,
            }
        },

        setLiveList(state, payload) {
            return {
                ...state,
                liveList: payload,
            }
        },

        setStarRankList(state, payload) {
            return {
                ...state,
                starRankList: payload,
            }
        },
    },

    effects: (dispatch) => ({

        async doFetchLiveDetail(payload, rootState) {
            const resp = await fetchLiveDetailReq(payload)

            handleResp(resp, dispatch, (data) => {
                if (data.ok) {
                    const userDetail = data.ok
                    dispatch.live.setLiveDetail(userDetail)
                }
            })
        },


        async doFetchLiveList(payload, rootState) {
            const resp = await fetchLiveListReq(payload)
            handleResp(resp, dispatch, (data) => {
                if (data.ok) {
                    // console.log(data.ok)
                    const list = data.ok.data
                    dispatch.live.setLiveList(list)
                }
            })
        },

        async doFetchStarRankList(payload, rootState) {
            payload.uid = uid
            const resp = await fetchStarRankListReq(payload)
            handleResp(resp, dispatch, (data) => {
                // console.log(data)
            })
        },

        async doFetchStarRankListNoToken(payload, rootState) {
            const resp = await fetchStarRankListNoTokenReq(payload)
            handleResp(resp, dispatch, (data) => {
            })
        },
    }),
}
